capture log close
clear all
set maxvar 30000
set more off
pause off
# delimit;

/* 
Figure B16
*/

local data "" ;
local output "" ;

local type_list_key "char genr nogn drug viol prop povc dgsl prbg prfd prtf vrob vcps" ;
local seg_1 "2002-2017" ;
local seg_2 "2007-2017" ;


/* Prep for bar graph: same crime */
use "prworapersist_bw264_crime_poly1allcov_iv_all.dta", clear ;	

rename n_age18unfav b ;
rename sen_age18unfav se ;
keep variables b se mean_rd ;

gen temp = substr(variables,1,10) ;

gen keep = . ;
foreach type in `type_list_key' { ;
	replace keep = 1 if temp == "order_`type'" ;
} ;

keep if keep == 1 ; 

gen time = substr(variables,-4,4) ;
gen stub = substr(variables,1,12) ;

keep stub time b se mean_rd ;

replace time = "early" if time == "arly" ;
replace time = "earlyb" if time == "rlyb" ;
replace time = "lateany" if time == "eany" ;
replace time = "lateany_n" if time == "ny_n" ;
replace time = "lateany_y" if time == "ny_y" ;
replace time = "lateall" if time == "ateb" | time == "lateb" ;
replace time = "lateonly" if time == "late" ;

rename b b_ ;
rename se se_ ;
rename mean_rd mean_ ;
drop if stub == "order_viol_n" ;

reshape wide 
	b se mean
	, i(stub) j(time) string ;

gen type = substr(stub,7,4) ;
gen temp = substr(stub,-2,2) ;
gen seg = 1 if temp == "_1" ;
replace seg = 2 if temp == "_2" ;
drop temp ;

gen hi95_lateall = b_lateall + 1.96*se_lateall ;
gen lo95_lateall = b_lateall - 1.96*se_lateall ;

order type seg b_lateonly b_both b_lateall mean_lateonly mean_both se_lateonly se_both ;

tempfile same ;
save `same' ;

		
/* Prep for bar graph: any crime */
use "prworapersist_bw264_crime_poly1allcov_iv_all.dta", clear ;	

rename n_age18unfav b ;
rename sen_age18unfav se ;
keep variables b se mean_rd ;

gen temp1 = substr(variables,1,5) ;
gen temp2 = substr(variables,-7,7) ;
keep if temp1 == "order" & (temp2 == "lateany" | temp2 == "teany_n" | temp2 == "teany_y" | 
							temp2 == "_w_15_5" | temp2 == "_w_15_7" | temp2 == "y_w_5_5" | temp2 == "y_w_5_7") ;

gen 	time = "lateall" 				if temp2 == "lateany" ;
replace time = "lateonly" 				if temp2 == "teany_n" ;
replace time = "both" 					if temp2 == "teany_y" ;
replace time = "lateonly_work_15_5" 	if temp2 == "_w_15_5" ;
replace time = "lateonly_work_15_7" 	if temp2 == "_w_15_7" ;
replace time = "lateonly_work_5_5" 		if temp2 == "y_w_5_5" ;
replace time = "lateonly_work_5_7" 		if temp2 == "y_w_5_7" ;
drop temp* ;

gen type = substr(variables,7,4) ;
gen stub = substr(variables,1,12) ;

keep stub time b se mean_rd type ;

rename b b_ ;
rename se se_ ;
rename mean_rd mean_ ;

reshape wide 
	b se mean
	, i(stub) j(time) string ;

gen temp = substr(stub,-2,2) ;
gen seg = 1 if temp == "_1" ;
replace seg = 2 if temp == "_2" ;
drop temp ;

gen hi95_lateall = b_lateall + 1.96*se_lateall ;
gen lo95_lateall = b_lateall - 1.96*se_lateall ;

tempfile any ;
save `any' ;


gen sort = . ;
local count = -1 ;
foreach type in genr dgsl prbg prfd prtf vrob vcps { ;
	replace sort = `count' + 2 if type == "`type'" ;
	local count = `count' + 2 ;
} ;


/* Put any and same together */
use `same', clear ;
gen precede = "same" ;
append using `any' ;
replace precede = "any" if precede == "" ;
keep type seg precede *lateonly* *lateall* *both* ;


gen sort = . ;
local count = -1 ;
foreach type in genr dgsl prbg prfd prtf vrob vcps { ;
	replace sort = `count' + 2 if type == "`type'" & precede == "same" ;
	replace sort = `count' + 3 if type == "`type'" & precede == "any" ;
	local count = `count' + 3 ;
} ;

use `any', clear ;
foreach var of varlist _all { ;
	rename `var' any_`var' ;
} ;
	rename any_type type ;
	rename any_seg seg ;
merge 1:1 type seg using `same' ;
keep type seg any*lateonly* any*lateall* any*both* *lateonly* ;
foreach coef in b se mean { ;
	rename `coef'_lateonly same_`coef'_lateonly ;
} ;


gen sort = . ;
local count = -1 ;
foreach type in genr dgsl prbg prfd prtf vrob vcps { ;
	replace sort = `count' + 2 if type == "`type'" ;
	local count = `count' + 2 ;
} ;

foreach seg in 2 { ;
twoway 
		(bar any_b_lateall same_b_lateonly any_b_lateonly any_b_lateonly_work_5_7 sort 			if seg == `seg' & sort != ., color(red*1.5 red orange green)) 
		(rcap any_hi95_lateall any_lo95_lateall sort 	if seg == `seg' & sort != ., color(orange*.5))
		
		(pcarrowi .08  1.6 .09   2 "", 	mstyle(none) lwidth(vthin) color(black))
		(pcarrowi .045 1.6 .055  2 "", 	mstyle(none) lwidth(vthin) color(black))
		(pcarrowi .018 1.6 .025  2 "", 	mstyle(none) lwidth(vthin) color(black))
		(pcarrowi .008 1.6 .015  2 "", 	mstyle(none) lwidth(vthin) color(black))
		
		, 
		title("Effects on later-period crime, decomposed by prior charge/employment", size(medsmall))
		legend(off)
		xsc(r(0 14)) xlabel(1 `" "All" "income-gen" "'  3 `" "Drug" "distribution" "' 5 "Burglary" 7 `" "Fraud/" "forgery" "' 9 "Theft" 11 "Robbery" 13 "Prostitution"
			, notick labsize(small)) 
		xtitle("") 
		ysc(r(-.025 .15)) ylab(0(.05).15) ytitle("IV estimate of SSI removal on `seg_`seg'' crime", size(medsmall))
		graphregion(color(white))
		bgcolor(white)
		ylabel(,nogrid angle(0))
		yline(0,lcolor(gray))
		plotregion(margin(0 0 0 0))
		ttext(.095 3.1 "Prior charge, same type", size(vsmall))
		ttext(.06  3 "Prior charge, different", size(vsmall))
		ttext(.03 2.9 "No prior charge" "or employment", size(vsmall))
		ttext(.017 3 "Prior employment," "no prior charge", size(vsmall))
		;
		graph export "`output'/figB16.png", width(1000) height(750) replace ;
} ;
